// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { InviteUser, RoomUser } from "../bean/GameBean";
import { GlobalBean } from "../bean/GlobalBean";
import NickNameLabel from "../util/NickNameLabel";
import { Tools } from "../util/Tools";

const {ccclass, property} = cc._decorator;

@ccclass
export default class SeatItemController extends cc.Component {

    @property(cc.Node)
    avatarFrame: cc.Node = null;
    @property(cc.Node)
    avatar: cc.Node = null;
    @property(cc.Node)
    seatEmpty: cc.Node = null;
    @property(NickNameLabel)
    nameLabel: NickNameLabel = null;
    @property(cc.Node)
    ready: cc.Node = null;
    @property(cc.Node)
    kickOutButton: cc.Node = null; // 踢人按钮节点

    users: InviteUser
    private kickCallback: Function = null; // 踢人回调函数

    // onLoad () {}

    start () {
        // 设置踢人按钮点击事件
        if (this.kickOutButton) {
            Tools.setTouchEvent(this.kickOutButton, () => {
                this.onKickOutClick();
            });
        }
    }

    setData(users: InviteUser) {
        this.users = users
        if (users == null) {
            this.nameLabel.string = window.getLocalizedStr('Empty')
            this.nameLabel.node.color = cc.Color.fromHEX(new cc.Color(),'#F4C684');
            this.seatEmpty.active = true;
            this.avatarFrame.active = false;
            this.avatar.active = false;
            this.ready.active = false;
            // 空座位时隐藏踢人按钮
            if (this.kickOutButton) {
                this.kickOutButton.active = false;
            }
        } else {
            this.nameLabel.string = users.nickname
            this.seatEmpty.active = false;
            this.avatarFrame.active = true;
            this.avatar.active = true;
            this.ready.active = users.ready
            this.nameLabel.node.color = cc.Color.fromHEX(new cc.Color(),'#D07649');
            Tools.setNodeSpriteFrameUrl(this.avatar, users.avatar)
            
            // 控制踢人按钮的显示：只有房主才能看到踢人按钮，且不能踢自己
            if (this.kickOutButton) {
                const isRoomOwner = GlobalBean.GetInstance().inviteInfo && 
                                   GlobalBean.GetInstance().inviteInfo.creatorId === GlobalBean.GetInstance().loginData.userInfo.userId;
                const isNotSelf = users.userId !== GlobalBean.GetInstance().loginData.userInfo.userId;
                this.kickOutButton.active = isRoomOwner && isNotSelf;
            }
        }
    }
    getUsers(){
        return this.users;
    }

    // 设置踢人回调函数
    setKickCallback(callback: Function) {
        this.kickCallback = callback;
    }

    // 踢人按钮点击处理
    private onKickOutClick() {
        if (this.users && this.kickCallback) {
            this.kickCallback(this.users.userId, this.users.nickname);
        }
    }

    // update (dt) {}
}
